package com.ternnetwork.workflow.service.impl;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.FormService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.ternnetwork.commons.model.http.BaseResponse;
import com.ternnetwork.workflow.dto.ProcessDefinitionQueryDto;
import com.ternnetwork.workflow.service.ProcessDefinitionService;
import com.ternnetwork.workflow.vo.ProcessDefinitionVo;



/**
 * 流程定义服务类
 * 
 * @author xuwenfeng
 *
 */
@Service
public class ProcessDefinitionServiceImpl implements ProcessDefinitionService {
	
	@Autowired
	private RepositoryService repositoryService;
	
	@Autowired
	private FormService formService;

    /**
     * 布署流程
     * @param resourceName
     * @param is
     * @return
     */
	@Override
	public BaseResponse deploy(String resourceName,InputStream fileInputStream){
		
		Deployment t=repositoryService.createDeployment().addInputStream(resourceName, fileInputStream).deploy();
		
		BaseResponse baseResponse=new BaseResponse();
		
		baseResponse.setSccucess(t);
		
		return baseResponse;
	}
	
	/**
	 * 挂起流程
	 * @param id
	 */
	@Override
	public BaseResponse suspendProcessDefinitionById(String id){
		
		repositoryService.suspendProcessDefinitionById(id);
		
	    BaseResponse baseResponse=new BaseResponse();
		
		baseResponse.setSccucess();
		
		return baseResponse;
	}
	/**
	 * 激活已挂起流程
	 * @param id
	 */
	@Override
	public BaseResponse activateProcessDefinitionById(String id){
		
		repositoryService.activateProcessDefinitionById(id);
		
        BaseResponse baseResponse=new BaseResponse();
		
		baseResponse.setSccucess();
		
		return baseResponse;
	}
	
	/**
	 * 删除流程和所有的流程实例和历史
	 */
	@Override
	public BaseResponse deleteDeploymentCascade(String id){
		
		repositoryService.deleteDeployment(id,true);
		
		BaseResponse baseResponse=new BaseResponse();
			
		baseResponse.setSccucess();
			
		return baseResponse;
    }
	
	/**
	 * 得到流程图
	 * @param id
	 * @return
	 */
	@Override
	public InputStream getDiagram(String id){
		return repositoryService.getProcessDiagram(id);
	}
	

	/**
	 * 分页查找
	 * @param pageable
	 * @param dto
	 * @return
	 */
	@Override
	public BaseResponse findByPage(Pageable pageable,ProcessDefinitionQueryDto dto) {
		
		
		ProcessDefinitionQuery q=repositoryService.createProcessDefinitionQuery();
		
		if(!StringUtils.isEmpty(dto.getName())){
			q.processDefinitionNameLike(dto.getName());
		}
		
		if(!StringUtils.isEmpty(dto.getKey())){
			q.processDefinitionKeyLike(dto.getKey());
		}
		
		if(!StringUtils.isEmpty(dto.getCategory())){
			q.processDefinitionCategoryLike(dto.getCategory());
		}
		
		int firstResult=Integer.valueOf(String.valueOf(pageable.getOffset()));
		
		int maxResult=pageable.getPageSize();
		
		List<ProcessDefinition> list=q.listPage(firstResult,maxResult);
		
		long totalElements=q.count();
		
		List<ProcessDefinitionVo> processDefinitionVoList=convertToProcessDefinitionVo(list);
		
		Map<String,Object> data=new HashMap<String,Object>(10);
		
		data.put("content", processDefinitionVoList);
		
		data.put("totalElements", totalElements);
		
		
		BaseResponse baseResponse=new BaseResponse();
		
		baseResponse.setSccucess(data);
		
		return baseResponse;
		
	}
	
	
	private List<ProcessDefinitionVo> convertToProcessDefinitionVo(List<ProcessDefinition> list){
		List<ProcessDefinitionVo> voList=new ArrayList<ProcessDefinitionVo>();
		for(ProcessDefinition pd:list){
			ProcessDefinitionVo vo=new ProcessDefinitionVo();
			vo.setId(pd.getId());
			vo.setName(pd.getName());
			vo.setKey(pd.getKey());
			vo.setCategory(pd.getCategory());
			vo.setDeploymentId(pd.getDeploymentId());
			vo.setDiagramResourceName(pd.getDiagramResourceName());
			vo.setVersion(String.valueOf(pd.getVersion()));
			vo.setSuspended(pd.isSuspended());
			voList.add(vo);
		}
		return voList;
	}
	
	/**
	 * 按ID查找流程定义
	 * @param processDefinitionId
	 * @return
	 */
	@Override
	public BaseResponse getProcessDefinitionById(String processDefinitionId){
		
		List<ProcessDefinition> list = getById(processDefinitionId);
		
        BaseResponse baseResponse=new BaseResponse();
		
		baseResponse.setSccucess(list);
		
		return baseResponse;
	}
	
	@Override
	public ProcessDefinition getOne(String processDefinitionId) {
		List<ProcessDefinition> list=getById(processDefinitionId);
		return list.size()==0? null:list.get(0);
	}
	

	private List<ProcessDefinition> getById(String processDefinitionId) {
		
		ProcessDefinitionQuery q=repositoryService.createProcessDefinitionQuery();
		
		q.processDefinitionId(processDefinitionId);
		
		List<ProcessDefinition> list=q.list();
		return list;
	}
	
	@Override
	public BaseResponse getStartFormKey(String processDefinitionId){
    	
		String formKey=formService.getStartFormKey(processDefinitionId);
    	
        BaseResponse baseResponse=new BaseResponse();
		
		baseResponse.setSccucess(formKey);
		
		return baseResponse;
    }

}
